///*
// *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
// *
// *  Redistribution and use in source and binary forms, with or without
// *  modification, are permitted provided that the following conditions are met:
// *
// *  Redistributions of source code must retain the above copyright notice,
// *  this list of conditions and the following disclaimer.
// *  Redistributions in binary form must reproduce the above copyright
// *  notice, this list of conditions and the following disclaimer in the
// *  documentation and/or other materials provided with the distribution.
// *  Neither the name of the dreamlu.net developer nor the names of its
// *  contributors may be used to endorse or promote products derived from
// *  this software without specific prior written permission.
// *  Author: Chill 庄骞 (smallchill@163.com)
// */
//package com.plian.system.service.fr.vehicle.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.dto.fr.vehicle.VehicleEliminateDTO;
//import com.plian.system.entity.fr.vehicle.VehicleEliminate;
//import com.plian.system.entity.fr.vehicle.VehicleInfo;
//import com.plian.system.enumeratea.CarStatus;
//import com.plian.system.mapper.fr.vehicle.VehicleEliminateMapper;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.ObjResult;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.mybatis.search.SearchEntity;
//import com.plian.system.mybatis.search.SearchParamEntiy;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.service.fr.vehicle.IVehicleEliminateService;
//import com.plian.system.service.fr.vehicle.IVehicleInfoService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.sys.orgzation.OrgzationService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.fr.vehicle.VehicleEliminateVO;
//import com.plian.system.vo.report.HeaderVO;
//import com.plian.system.vo.report.ReportReturnVO;
//import com.plian.system.vo.report.ReportVO;
//import com.plian.system.wrapper.fr.veihicle.VehicleEliminateWrapper;
//import lombok.AllArgsConstructor;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//import static com.plian.system.constant.sys.FormTypeConstant.FR_VEHICLEELIMINATE_FORM;
//
///**
// * 车辆淘汰管理 服务实现类
// *
// * @author jianglei
// * @since 2019-09-30
// */
//@Service
//@AllArgsConstructor
//public class VehicleEliminateServiceImpl extends BaseServiceImpl<VehicleEliminateMapper, VehicleEliminate> implements IVehicleEliminateService, BaseFlowService {
//
//	private IVehicleInfoService vehicleInfoService;
//
//	private FormWorkflowService formWorkflowService;
//
//	private IFormCodeService formCodeService;
//
//	private VehicleEliminateWrapper vehicleEliminateWrapper;
//
//	private static final String SCRAP_STR = "淘汰";
//
//	private static final String UPDATE_STR = "更新";
//
//	@Override
//    public boolean saveOrUpdate(VehicleEliminate vehicleEliminate) {
//
//		if (vehicleEliminate.getId() == null){
//			vehicleEliminate.setVersion("1");
//			vehicleEliminate.setCode(formCodeService.getCode(FormTypeConstant.FR_VEHICLEELIMINATE_FORM));
//			vehicleEliminate.setFormType(String.valueOf(FormTypeConstant.FR_VEHICLEELIMINATE_FORM));
//		}
//        return super.saveOrUpdate(vehicleEliminate);
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//		List<Long> vehicleInfoIds = new ArrayList<>();
//		for (Long id : ids) {
//			VehicleEliminate vehicleEliminate = getById(id);
//			if (vehicleEliminate == null) {
//				return false;
//			} else if (vehicleEliminate.getStatus() != null &&
//					(vehicleEliminate.getStatus() != FormStatusConstant.STASH && vehicleEliminate.getStatus() != FormStatusConstant.UNREVIEWED)) {
//				return false;
//			}
//			vehicleInfoIds.add(vehicleEliminate.getVehicleInfoId());
//			formWorkflowService.deleteProcessInstance(String.valueOf(id));
//		}
//		vehicleInfoService.updateStatus(vehicleInfoIds, CarStatus.USING.getStatus());
//    return super.deleteLogic(ids);
//    }
//
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//		//将需要提交的流程变量返回，用于工作流侧
//		return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//		VehicleEliminate vehicleEliminate = getById(id);
//
//		if (vehicleEliminate.getIsContinueUse() != null && vehicleEliminate.getIsContinueUse() == 1){
//			vehicleEliminate.setNumberPlateStatus(SCRAP_STR);
//		}
//		Date now = DateUtil.now();
//		vehicleEliminate.setApprovedTime(now);
//		updateById(vehicleEliminate);
//		/**
//		 * 修改该车状态为报废
//		 */
//		VehicleInfo vehicleInfo = vehicleInfoService.getById(vehicleEliminate.getVehicleInfoId());
//		if (Optional.ofNullable(vehicleInfo).isPresent()){
//			vehicleInfo.setStatus(CarStatus.SCRAP.getStatus());
//			vehicleInfo.setIsContinueUse(vehicleEliminate.getIsContinueUse());
//			vehicleInfo.setDef14(now);
//			vehicleInfoService.updateById(vehicleInfo);
//		}
//
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    public void cancel(Long id) {
//		VehicleEliminate vehicleEliminate = getById(id);
//		QueryWrapper<VehicleInfo> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("id",vehicleEliminate.getVehicleInfoId());
//		VehicleInfo vehicleInfo = vehicleInfoService.getOne(queryWrapper);
//		if (Optional.ofNullable(vehicleInfo).isPresent()){
//			vehicleInfo.setStatus(CarStatus.AUDIT.getStatus());
//			vehicleInfoService.updateById(vehicleInfo);
//		}
//    }
//
//	/**
//     * 统一处理状态机事件方法
//     * @param id 主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            VehicleEliminate vehicleEliminate = getById(id);
////            if (vehicleEliminate.getStatus() != judgeStatus) {
////                 return false;
////             }
//			vehicleEliminate.setStatus(updateStatus);
//            updateById(vehicleEliminate);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//	@Override
//	public Boolean checkIsExisted(Long id, String code) {
//		boolean result = true;
//		if (Optional.ofNullable(code).isPresent()) {
//			QueryWrapper<VehicleEliminate> queryWrapper = new QueryWrapper<>();
//			queryWrapper.eq("code", code);
//			VehicleEliminate vehicleEliminate = getOne(queryWrapper);
//			if (Optional.ofNullable(vehicleEliminate).isPresent()) {
//				if (id == null || !id.equals(vehicleEliminate.getId())){
//					result =  false;
//				}
//			}
//		}
//		return result;
//	}
//
//	@Override
//	public Boolean checkNumberPlate(Long id, String numberPlate) {
//		if (Optional.ofNullable(numberPlate).isPresent()) {
//
//			/**
//			 * 查询该车牌是否在审核中
//			 */
//			QueryWrapper<VehicleEliminate> queryWrapper = new QueryWrapper<>();
//			queryWrapper.eq("number_plate", numberPlate);
//			queryWrapper.and(wrapper -> wrapper.eq("status", FormStatusConstant.UNREVIEWED)
//				.or().eq("status", FormStatusConstant.FLOW));
//			VehicleEliminate vehicleEliminate = getOne(queryWrapper);
//
//			if (Optional.ofNullable(vehicleEliminate).isPresent()) {
//				if (id == null || !id.equals(vehicleEliminate.getId())){
//					return false;
//				}
//			}
//
//			/**
//			 * 校验该车牌是否存在和状态
//			 */
//			if (id == null || !numberPlate.equals(getById(id).getNumberPlate())){
//				QueryWrapper<VehicleInfo> vehicleInfoQueryWrapper = new QueryWrapper<>();
//				vehicleInfoQueryWrapper.eq("number_plate", numberPlate);
//				vehicleInfoQueryWrapper.eq("status", CarStatus.USING.getStatus());
//				VehicleInfo vehicleInfo = vehicleInfoService.getOne(vehicleInfoQueryWrapper);
//				if (!Optional.ofNullable(vehicleInfo).isPresent()) {
//					return false;
//				}
//			}
//		}
//
//		return true ;
//	}
//
//	@Override
//	@Transactional(rollbackFor=RuntimeException.class)
//	public Boolean submit(VehicleEliminate vehicleEliminate, int type) {
//		/**
//		 * 新增或修改车牌时要对车辆信息进行赋值
//		 */
//
//		QueryWrapper<VehicleInfo> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("number_plate", vehicleEliminate.getNumberPlate());
//		queryWrapper.eq("status", CarStatus.USING.getStatus());
//
//		VehicleInfo vehicleInfo = vehicleInfoService.getOne(queryWrapper);
//		if (Optional.ofNullable(vehicleInfo).isPresent()){
//			if (type == 1){
//				vehicleInfo.setStatus(CarStatus.AUDIT.getStatus());
//			}else {
//				vehicleInfo.setStatus(CarStatus.SCRAP.getStatus());
//			}
//			vehicleInfoService.updateById(vehicleInfo);
//
//			vehicleEliminate.setBrand(vehicleInfo.getBrand());
//			vehicleEliminate.setVehicleInfoId(vehicleInfo.getId());
//			vehicleEliminate.setCarModelId(vehicleInfo.getCarModelId());
//			vehicleEliminate.setApplicationId(vehicleInfo.getApplicationId());
//			vehicleEliminate.setExhaustVolume(vehicleInfo.getExhaustVolume());
//			vehicleEliminate.setCompanyId(vehicleInfo.getCompanyId());
//		}
//
//
//		if (vehicleEliminate.getId() != null){
//			VehicleEliminate original = getById(vehicleEliminate.getId());
//			if (!original.getNumberPlate().equals(vehicleEliminate.getNumberPlate())){
//				List<Long> vehicleInfoIds = new ArrayList<>();
//				vehicleInfoIds.add(original.getVehicleInfoId());
//				vehicleInfoService.updateStatus(vehicleInfoIds, CarStatus.USING.getStatus());
//			}
//		}
//
//		if (!Optional.ofNullable(vehicleEliminate.getStatus()).isPresent()){
//			vehicleEliminate.setStatus(FormStatusConstant.UNREVIEWED);
//		}
//
//		boolean result = saveOrUpdate(vehicleEliminate);
//
//		HashMap<String, Object> processVariable = new HashMap<>();
//		formWorkflowService.startProcessAndSimulate(String.valueOf(vehicleEliminate.getId()),vehicleEliminate.getFormType(),processVariable);
//
//		return result;
//	}
//
//
//
//	@Override
//	public String checkSubmit(VehicleEliminate vehicleEliminate) {
//
//		if (!StringUtil.isEmpty(vehicleEliminate.getCode())&&!checkIsExisted(vehicleEliminate.getId(), vehicleEliminate.getCode())){
//			return "单据编号已存在";
//		}
//		if (!checkNumberPlate(vehicleEliminate.getId(), vehicleEliminate.getNumberPlate())){
//			return "该车牌不存在或该车不能报废";
//		}
//		return null;
//	}
//	@Override
//	public java.io.File exportExcel(JSONObject jsonObject) {
//		//todo excel导出未处理
//		List<SearchEntity> entityList = Optional.ofNullable(jsonObject.getJSONArray("query"))
//			.map(x -> x.toJavaList(SearchEntity.class)).orElse(new ArrayList<>());
//		QueryWrapper<VehicleEliminate> qw= SearchParamEntiy.getQueryWrapper(
//			Condition.getQueryWrapper(new VehicleEliminate()), entityList
//		);
//
//		List<VehicleEliminate> floors = list(qw);
//		//return ExcelUtils.exportExcelByList(jsonObject, VehicleEliminateWrapper.build().listVO(floors));
//		return  null;
//	}
//
//
//	@Override
//	public ObjResult change(VehicleEliminate vehicleEliminate) {
//		ObjResult objResult;
//		PageData pageData = new PageData();
//		VehicleEliminate old = getById(vehicleEliminate.getId());
//		List<Long> vehicleInfoIds = new ArrayList<>();
//		vehicleInfoIds.add(old.getVehicleInfoId());
//		vehicleInfoService.updateStatus(vehicleInfoIds, CarStatus.USING.getStatus());
//
//		String result = checkSubmit(vehicleEliminate);
//		if (Optional.ofNullable(result).isPresent()) {
//			vehicleInfoService.updateStatus(vehicleInfoIds, CarStatus.SCRAP.getStatus());
//			objResult = new ObjResult(CommonCode.FAIL);
//			pageData.put("message", result);
//			objResult.setPageData(pageData);
//			return objResult;
//		}
//		//snapshotClient.push("vehicleEliminate", vehicleEliminate.getId(), jsonObject);
//		vehicleEliminate.setVersion(StringUtil.isBlank(vehicleEliminate.getVersion()) ?
//			"1" : String.valueOf(Integer.valueOf(vehicleEliminate.getVersion())+1));
//		if (submit(vehicleEliminate, 2)){
//			objResult = new ObjResult(CommonCode.SUCCESS);
//		}else {
//			objResult = new ObjResult(CommonCode.FAIL);
//		}
//		return objResult;
//	}
//
//	private OrgzationService orgzationService;
//
//	@Override
//	public List<PageData> findByIds(List<String> formIds) {
//		QueryWrapper<VehicleEliminate> queryWrapper = new QueryWrapper<>();
//		queryWrapper.in("id", Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//		List<VehicleEliminate> vehicleEliminates = list(queryWrapper);
//		List<VehicleEliminateVO> vehicleLeaseVOS = vehicleEliminateWrapper.entityToVO(vehicleEliminates);
//		List<PageData> pageDataList = new ArrayList<>();
//		try{
//			if (CollectionUtil.isNotEmpty(vehicleLeaseVOS)){
//				for (VehicleEliminateVO vo : vehicleLeaseVOS){
//					PageData pd = new PageData();
//					if (vo.getVehicleInfoId() != null){
//						VehicleInfo vehicleInfo = vehicleInfoService.getById(vo.getVehicleInfoId());
//						if(vehicleInfo != null && StringUtil.isNotBlank(vehicleInfo.getCompanyId())) {
//							PageData org = orgzationService.getCompanyNameById(vehicleInfo.getCompanyId());
//							if (org != null) {
//								pd.put("UNITNAME", org.getString("name"));
//								pd.put("orgName", org.getString("name"));
//							}
//							pd.put("applyOrganizationId", vehicleInfo.getCompanyId());
//						}
//					}
//					pd.put("id", vo.getId());
//					pd.put("APPLICANTNAME", vo.getCreateUserName());
//					pd.put("applicantId", vo.getCreateUser());
//					pd.put("formType", FormTypeConstant.FR_VEHICLEELIMINATE_FORM);
//
//					pageDataList.add(pd);
//				}
//			}
//
//			return pageDataList;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//		}
//		return pageDataList;
//	}
//
//	@Override
//	public void goBackToStash(String formId) {
//		cancel(Long.valueOf(formId));
//		handleStatus(Long.valueOf(formId), UNREVIEWED);
//	}
//
//	@Override
//	public List<Integer> getFormType() {
//		return Arrays.asList(FR_VEHICLEELIMINATE_FORM);
//	}
//
//
//	@Override
//	public ReportReturnVO report(VehicleEliminateDTO vehicleEliminateDTO) {
//		List<VehicleEliminateVO> data = baseMapper.report(vehicleEliminateDTO);
//		ReportReturnVO reportReturnVO = new ReportReturnVO();
//		ReportVO reportVO = new ReportVO();
//		reportVO
//				.addHeader(HeaderVO.builder().key("companyName").title("所属单位").build())
//				.addHeader(HeaderVO.builder().key("numberPlate").title("车牌号").build())
//				.addHeader(HeaderVO.builder().key("brand").title("品牌").build())
//				.addHeader(HeaderVO.builder().key("exhaustVolume").title("排气量(cc)").build())
//				.addHeader(HeaderVO.builder().key("carModelName").title("车型").build())
//				.addHeader(HeaderVO.builder().key("applicationName").title("用途").build())
//				.addHeader(HeaderVO.builder().key("disposalMethodName").title("处置方式").build())
//				.addHeader(HeaderVO.builder().key("outTime").title("淘汰时间").build())
//				.addHeader(HeaderVO.builder().key("disposalPrice").title("处置价格（万元）").build())
//				.addHeader(HeaderVO.builder().key("remark").title("备注").build());
//		reportVO.setData(data);
//		reportReturnVO.setGrids(reportVO);
//		return reportReturnVO;
//	}
//}
//
//
